package com.mqqzt.douban;

import java.util.List;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mqqzt.douban.domain.NewBook;
import com.mqqzt.douban.util.LoadImageAsyncTask;
import com.mqqzt.douban.util.LoadImageAsyncTask.LoadImageCallback;
import com.mqqzt.douban.util.NetUtil;


public class NewBookActivity extends BaseActivity implements OnClickListener {
	private ListView subjectlist;
	private List<NewBook> books;
	private NewBookAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.subject);
		super.onCreate(savedInstanceState);
	}
	@Override
	public void setupView() {
		subjectlist = (ListView) this.findViewById(R.id.subjectlist);
		mTextViewTitle = (TextView) this.findViewById(R.id.myTitle);
		mRelativeLayoutLoading = (RelativeLayout) this.findViewById(R.id.loading);
		mImageBack = (ImageButton) this.findViewById(R.id.back_button);
		
	}

	@Override
	public void setListener() {
		mImageBack.setOnClickListener(this);
		
	}

	@Override
	public void fillData() {
		mTextViewTitle.setText("豆瓣新书");
		new AsyncTask<Void, Void, Boolean>() {
			
			@Override
			protected void onPreExecute() {
				showLoading();
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(Boolean result) {
				super.onPostExecute(result);
				hideLoading();
				if(result){
					if(adapter == null){
						adapter = new NewBookAdapter();
						subjectlist.setAdapter(adapter);
					}else{
						adapter.notifyDataSetChanged();
					}
					
				}else{
					Toast.makeText(NewBookActivity.this, "获取服务器数据失败，请检查网络", Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			protected Boolean doInBackground(Void... params) {
				try {
					books = NetUtil.getNewBooks(getApplicationContext());
					return true;
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
				
			}
		}.execute();
		
	}
	
	private class NewBookAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return books.size();
		}

		@Override
		public Object getItem(int position) {
			return books.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			NewBook book = books.get(position);
			View view = null;
			if(convertView == null){
				view = View.inflate(NewBookActivity.this, R.layout.new_book_item, null);
			}else{
				view = convertView;
			}
			LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll_book_image);
			final ImageView iv = new ImageView(NewBookActivity.this);
			ll.removeAllViews();
			ll.addView(iv, new LayoutParams(60, 80));
			TextView tv_title = (TextView) view.findViewById(R.id.book_title);
			TextView tv_description = (TextView) view.findViewById(R.id.book_description);
			tv_title.setText(book.getName());
			tv_description.setText(book.getDescription());
			SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
			boolean canloadicon = sp.getBoolean("canloadicon", true);
			if(canloadicon){
				LoadImageAsyncTask task = new LoadImageAsyncTask(new LoadImageCallback() {
					
					@Override
					public void beforeLoadImage() {
						iv.setImageResource(R.drawable.book);
						
					}
					
					@Override
					public void afterLoadImage(Bitmap bitmap) {
						if(bitmap != null){
							iv.setImageBitmap(bitmap);
						}else{
							iv.setImageResource(R.drawable.book);
						}
						
					}
				});
				task.execute(book.getImagePath());
			}else{
				iv.setImageResource(R.drawable.book);
			}
			
			return view;
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "设置界面");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(this, SettingActivity.class);
		startActivity(intent);
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_button:
			finish();
			break;

		}
		
	}

}
